package com.mr.base.alpha.mappers;

import com.mr.base.alpha.dos.OrganizationDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.math.BigDecimal;

import com.mr.base.core.mybatis.annotation.*;
import com.mr.base.alpha.params.OrganizationParam;

public interface OrganizationMapper {

    @Select({"<script>",
            " select",
            " id, created_id, created_name, created_time, updated_id, updated_name, updated_time, status, organization_name, level, parentId ",
            " from t_organization ",
            "<where>",
            "<if test='param.id != null' > and id = #{param.id,jdbcType=BIGINT}</if>",
            "<if test='param.createdId != null' > and created_id = #{param.createdId,jdbcType=BIGINT}</if>",
            "<if test='param.createdName != null and param.createdName != \"\"' > and created_name = #{param.createdName,jdbcType=VARCHAR}</if>",
            "<if test='param.createdTime != null' > and created_time = #{param.createdTime,jdbcType=TIMESTAMP}</if>",
            "<if test='param.updatedId != null' > and updated_id = #{param.updatedId,jdbcType=BIGINT}</if>",
            "<if test='param.updatedName != null and param.updatedName != \"\"' > and updated_name = #{param.updatedName,jdbcType=VARCHAR}</if>",
            "<if test='param.updatedTime != null' > and updated_time = #{param.updatedTime,jdbcType=TIMESTAMP}</if>",
            "<if test='param.status != null' > and status = #{param.status,jdbcType=INTEGER}</if>",
            "<if test='param.organizationName != null and param.organizationName != \"\"' > and organization_name = #{param.organizationName,jdbcType=VARCHAR}</if>",
            "<if test='param.level != null' > and level = #{param.level,jdbcType=INTEGER}</if>",
            "<if test='param.parentid != null' > and parentId = #{param.parentid,jdbcType=BIGINT}</if>",
            "</where>",
            "</script>",
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "created_id", property = "createdId", jdbcType = JdbcType.BIGINT),
            @Result(column = "created_name", property = "createdName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_id", property = "updatedId", jdbcType = JdbcType.BIGINT),
            @Result(column = "updated_name", property = "updatedName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "updated_time", property = "updatedTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "organization_name", property = "organizationName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "parentId", property = "parentid", jdbcType = JdbcType.BIGINT),
    })
    List<OrganizationDO> listOrganization(@Param("param") OrganizationParam param);

    @Update({
            "update t_organization set is_deleted = true ",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Update({
            " <script> ",
            " update t_organization set is_deleted = true ",
            " where id  in ",
            " <foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> ",
            "  #{id} ",
            " </foreach>",
            " </script>",
    })
    int batchDeleteByPrimaryKeys(@Param("ids") List<Long> ids);

    @Insert({
            "insert into t_organization (",
            " id, created_id, created_name, created_time, updated_id, updated_name, updated_time, status, organization_name, level, parentId )",
            "values (",
            "#{id,jdbcType = BIGINT} , ",
            "#{createdId,jdbcType = BIGINT} , ",
            "#{createdName,jdbcType = VARCHAR} , ",
            "#{createdTime,jdbcType = TIMESTAMP} , ",
            "#{updatedId,jdbcType = BIGINT} , ",
            "#{updatedName,jdbcType = VARCHAR} , ",
            "#{updatedTime,jdbcType = TIMESTAMP} , ",
            "#{status,jdbcType = INTEGER} , ",
            "#{organizationName,jdbcType = VARCHAR} , ",
            "#{level,jdbcType = INTEGER} , ",
            "#{parentid,jdbcType = BIGINT} ",
            " )"
    })
    int insert(OrganizationDO record);

    @InsertProvider(type = OrganizationSqlProvider.class, method = "insertSelective")
    int insertSelective(OrganizationDO record);

    @Insert({
            "<script>",
            "insert into t_organization (",
            " id, created_id, created_name, created_time, updated_id, updated_name, updated_time, status, organization_name, level, parentId",
            ")",
            "values ",
            " <foreach collection=\"list\" item=\"item\" separator=\",\"> ",
            "(",
            " #{item.id,jdbcType=BIGINT} ,  ",
            " #{item.createdId,jdbcType=BIGINT} ,  ",
            " #{item.createdName,jdbcType=VARCHAR} ,  ",
            " #{item.createdTime,jdbcType=TIMESTAMP} ,  ",
            " #{item.updatedId,jdbcType=BIGINT} ,  ",
            " #{item.updatedName,jdbcType=VARCHAR} ,  ",
            " #{item.updatedTime,jdbcType=TIMESTAMP} ,  ",
            " #{item.status,jdbcType=INTEGER} ,  ",
            " #{item.organizationName,jdbcType=VARCHAR} ,  ",
            " #{item.level,jdbcType=INTEGER} ,  ",
            " #{item.parentid,jdbcType=BIGINT}  ",
            ")",
            " </foreach>",
            " </script>"
    })
    int batchInsert(@Param("list") List<OrganizationDO> doLists);

    @Select({
            "select",
            " id, created_id, created_name, created_time, updated_id, updated_name, updated_time, status, organization_name, level, parentId",
            "from t_organization ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "created_id", property = "createdId", jdbcType = JdbcType.BIGINT),
            @Result(column = "created_name", property = "createdName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_id", property = "updatedId", jdbcType = JdbcType.BIGINT),
            @Result(column = "updated_name", property = "updatedName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "updated_time", property = "updatedTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "organization_name", property = "organizationName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "parentId", property = "parentid", jdbcType = JdbcType.BIGINT),
    })
    OrganizationDO selectByPrimaryKey(Long id);

    @UpdateProvider(type = OrganizationSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(OrganizationDO record);

    @Update({
            "update t_organization set ",
            "id = #{id,jdbcType=BIGINT}, ",
            "created_id = #{createdId,jdbcType=BIGINT}, ",
            "created_name = #{createdName,jdbcType=VARCHAR}, ",
            "created_time = #{createdTime,jdbcType=TIMESTAMP}, ",
            "updated_id = #{updatedId,jdbcType=BIGINT}, ",
            "updated_name = #{updatedName,jdbcType=VARCHAR}, ",
            "updated_time = #{updatedTime,jdbcType=TIMESTAMP}, ",
            "status = #{status,jdbcType=INTEGER}, ",
            "organization_name = #{organizationName,jdbcType=VARCHAR}, ",
            "level = #{level,jdbcType=INTEGER}, ",
            "parentId = #{parentid,jdbcType=BIGINT}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(OrganizationDO record);

    @Update({
            " <script>",
            " <foreach collection=\"list\" item=\"item\" separator=\";\"> ",
            " update t_organization",
            " set",
            "created_id = #{item.createdId,jdbcType=BIGINT} ,  ",
            "created_name = #{item.createdName,jdbcType=VARCHAR} ,  ",
            "created_time = #{item.createdTime,jdbcType=TIMESTAMP} ,  ",
            "updated_id = #{item.updatedId,jdbcType=BIGINT} ,  ",
            "updated_name = #{item.updatedName,jdbcType=VARCHAR} ,  ",
            "updated_time = #{item.updatedTime,jdbcType=TIMESTAMP} ,  ",
            "status = #{item.status,jdbcType=INTEGER} ,  ",
            "organization_name = #{item.organizationName,jdbcType=VARCHAR} ,  ",
            "level = #{item.level,jdbcType=INTEGER} ,  ",
            "parentId = #{item.parentid,jdbcType=BIGINT}  ",
            " where id = #{item.id,jdbcType=BIGINT}",
            " </foreach></script>",
    })
    @IgnoreDelete
    @IgnoreTenant
    int batchUpdate(@Param("list") List<OrganizationDO> doLists);

}
